package example.spring.cn.autowire;

import example.spring.cn.DTO.FXNewsBean;
import example.spring.cn.autowire.service.IFXNewsListener;
import example.spring.cn.autowire.service.IFXNewsPersister;
import example.spring.cn.utils.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: Administrator
 * @Date: 上午 11:55 2019-04-21
 * @Description:
 */
@Component(value = "fxNewsProviderAuto")
public class FXNewsProviderAuto {
    @Autowired
    private IFXNewsListener newsListener;
    @Autowired
    private IFXNewsPersister newPersistener;

    public FXNewsProviderAuto(){}

    public FXNewsProviderAuto(IFXNewsPersister newPersistener, IFXNewsListener newsListener){
        this.newPersistener =  newPersistener;
        this.newsListener = newsListener;
    }

    public void setNewsListener(IFXNewsListener newsListener) {
        this.newsListener = newsListener;
    }

    public void setNewPersistener(IFXNewsPersister newPersistener) {
        this.newPersistener = newPersistener;
    }

    public void getAndPersistNews()
    {
        String[] newsIds = newsListener.getAvailableNewsIds();
        if(ArrayUtils.isEmpty(newsIds))
        {
            return;
        }
        for(String newsId : newsIds)
        {
            FXNewsBean newsBean = newsListener.getNewsByPK(newsId);
            newPersistener.persistNews(newsBean);
            newsListener.postProcessIfNecessary(newsId);
        }
    }
}
